<?php
namespace app\common\model;

use think\Db;
use think\Model;
use think\Session;
class LuntanVotecount extends Model
{
	protected $insert = ['create_time'];

	 /**
     * 自动生成时间
     * @return bool|string
     */
    protected function setCreateTimeAttr()
    {
        return strtotime('now');
    }

    public  function  getVotecountList($start_time,$end_time,$page,$votename)
    {
    	$map  = [];
    	$map1 = [];
    	if(!empty($start_time)){  
            $start_times = strtotime($start_time);          
            if(!empty($end_time)){
                $end_times = strtotime($end_time); 
                $map['create_time'] = ['between',"$start_times,$end_times"];
            }else{
                $map['create_time'] = ['>=',"$start_times"];
            }
        }else if(!empty($end_time)){
            $end_times = strtotime($end_time); 
            $map['create_time'] = ['<=',"$end_times"];
        }

        if (!empty($votename)) {
        	$map1['name'] = ['like','%{$votename}%'];
        }

        //先取出所有的投票的问题
        $votelist = Db('luntan_vote')->select();
        //循环获得选项
       	$i = 0;
       	for(;$i < count($votelist);$i++){
       		$votelist[$i]['categoryname'] = Db('luntan_category')->where('id',$votelist[$i]['cid'])->column('name');
       		$voteselectlist[$i] = Db('luntan_voteselect')->where('vid',$votelist[$i]['id'])->select();
       		//循环获得选项对应的投票总数
       		$j = 0;
       		for(;$j < count($voteselectlist[$i]);$j++){
       			$votecount  = $this->where('sid',$voteselectlist[$i][$j]['id'])->where($map)->count();
       			$totalcount = $this->where('vid',$voteselectlist[$i][$j]['vid'])->where($map)->count();
       			$votecountlist[$i][$j]['count'] 	   = $votecount;
       			$votecountlist[$i][$j]['votename']     = $votelist[$i]['name'];
       			$votecountlist[$i][$j]['selectname']   = $voteselectlist[$i][$j]['name'];
       			$votecountlist[$i][$j]['categoryname'] = $votelist[$i]['categoryname'][0];
       			if($totalcount){
       				$votecountlist[$i][$j]['percent']      = $votecount/$totalcount;
       			}else{
       				$votecountlist[$i][$j]['percent']      = 0;
       			}
       			
       		}
       	}
        return $votecountlist;

    }


}